<?php

defined('MYLITS') ? '' : exit('No direct script access allowed');

/**
 * @version     1.0.0
 * @package     mylits engine
 * @license     GNU General Public License version 3
 * @author      Arfan Fudyartanto D N <arfan@mylits.com> - http://engine.mylits.com
 */
class Mylits_Controller {

    public $task = '';
    private static $component_path = '';
    public $params = array();
    private static $component = '';
    public $is_public;
    public $base_url = '';

    public function __construct() {
        $Reflection = new ReflectionClass($this);
        $tmp = explode('/', str_replace('\\', '/', $Reflection->getFileName()));
        unset($tmp[count($tmp) - 1]);
        self::$component = end($tmp);
        self::$component_path = implode('/', $tmp) . '/';
        $this->base_url = BASE_URL . 'components/comp.' . $this->getComponentName() . '/';
        unset($tmp);
    }

    /**
     * Setting permission of the component. If component is public component will display in all user include guest
     * 
     * @param boolean $bool
     */
    public function setPublic($bool) {
        $this->is_public = $bool;
    }

    /**
     * Method to execute process in current component
     * 
     * @param string $process -> nameof process will be loaded
     * @param string $type -> 'view' or 'do', please use 'do' instead to load action process 
     * @return object extended Mylits_Process object
     */
    public function executeProcess($type, $process) {
        if ($type == 'view') {
            $process_path = self::$component_path . 'process/view.' . $process . '.proc.php';
        } else {
            $process_path = self::$component_path . 'process/do.' . $process . '.proc.php';
        }
        if (is_file($process_path)) {
            require_once $process_path;
            $class = $process . 'Process';
            $obj = new $class;
            $obj->params = $this->params;
            if(isset($this->widgetParams)){
                $obj->widgetParams = $this->widgetParams;
            }
            if (isset($this->widget_id)) {
                $obj->widget_id = $this->widget_id;
            }

            if ($type == 'view') {
                #$data = $obj->response();
                $data = call_user_func_array(array($obj, "response"), $this->params);
                return $obj->getContent($data);
            } else {
                call_user_func_array(array($obj, "response"), $this->params);
            }
        } else {
            exit('Process ' . $process . ' not found in ' . self::$component);
        }
    }

    /**
     * Method to get component name
     * 
     * @return string component name
     */
    public function getComponentName() {
        return str_replace('comp.', '', self::$component);
    }
}

?>
